package com.label.Service.Impl;

import cn.hutool.core.codec.Base64;
import com.label.Model.Query.TransQuery;
import com.label.Service.WordService;
import com.label.Utils.WordUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class WordServiceImpl implements WordService {

    @Value("${file.path}")
    public String path;

    @Resource
    private WordUtil wordUtil;

    @Override
    public String transToFile(TransQuery transQuery, HttpServletResponse response) throws Exception {
        Map<String,Object> test=new HashMap<>();
        test.put("name",transQuery.getBaseInfo().get("姓名"));
        test.put("sex",transQuery.getBaseInfo().get("性别"));
        test.put("userId",transQuery.getBaseInfo().get("学号"));
        test.put("credit",transQuery.getBaseInfo().get("二课总学分"));
        test.put("majorP",transQuery.getBaseInfo().get("专业排名"));
        test.put("classP",transQuery.getBaseInfo().get("班级排名"));
        test.put("GPA",transQuery.getBaseInfo().get("平均绩点"));
        test.put("unionposition",transQuery.getBaseInfo().get("学生会职务"));
        test.put("classposition",transQuery.getBaseInfo().get("班级职务"));
        test.put("unit",transQuery.getBaseInfo().get("学院"));
        test.put("major",transQuery.getBaseInfo().get("专业"));
        test.put("counselorid",transQuery.getBaseInfo().get("辅导员"));
        test.put("political",transQuery.getBaseInfo().get("政治面貌"));
        test.put("class",transQuery.getBaseInfo().get("班级"));
        test.put("image3",transQuery.getFiveStarBase64().replace("data:image/png;base64,",""));
        test.put("image2",transQuery.getGpaBase64().replace("data:image/png;base64,",""));

        test.put("secondClassP",transQuery.getBaseInfo().get("二课学分班级排名"));
        test.put("secondMajorP",transQuery.getBaseInfo().get("二课学分专业排名"));
        test.put("eval",transQuery.getEvaluateWords());

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日");
        test.put("date",simpleDateFormat.format(new Date()));

        if(transQuery.getPicPath()==null){
            test.put("image1","");
            String fileName = "default.png";
            try{
                String imageBase64=getData(fileName);
                test.put("image1",imageBase64);
            }catch (Exception e){
                test.put("image1","");
            }
        }

        else {
            String url=URLDecoder.decode(transQuery.getPicPath(),"UTF-8");
            if(url.equals("")){
                String fileName = "default.png";
                try{
                    String imageBase64=getData(fileName);
                    test.put("image1",imageBase64);
                }catch (Exception e){
                    test.put("image1","");
                }
            }else{
                String[] parts=url.split("/");
                String fileName=parts[parts.length-1];
                try{
                    String imageBase64=getData(fileName);
                    test.put("image1",imageBase64);
                }catch (Exception e){
                    test.put("image1","");
                }
            }

        }
        return wordUtil.exportDocFile("template.ftl",test);
    }


    private String getData(String fileName) throws Exception{
        File file = new File(path+fileName);
        FileInputStream fis = new FileInputStream(file);
        byte[] imgData = new byte[fis.available()];
        fis.read(imgData);
        fis.close();
        String imageBaser64=Base64.encode(imgData);
        String hou=getFileExtension(file);
        System.out.println(hou);
        switch (hou){
            case "png":return imageBaser64.replace("data:image/png;base64,","");
            case "jpg":return imageBaser64.replace("data:image/jpg;base64,","");
            case "jpeg":return imageBaser64.replace("data:image/jpeg;base64,","");
            default:return "";
        }
    }

    private static String getFileExtension(File file) {
        String fileName = file.getName();
        if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0)
            return fileName.substring(fileName.lastIndexOf(".")+1);
        else return "";
    }
}
